package com.clx.chat.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    public static final String QUEUE_NAME = "chat-que";
    public static final String EXCHANGE_NAME = "chat-exc";
    public static final String ROUTING_KEY = "chat-key";


    @Value("${spring.rabbitmq.host}")
    public String Host;
    @Value("${spring.rabbitmq.port}")
    public int Port;
    @Value("${spring.rabbitmq.username}")
    public String RabbitmqUserName;
    @Value("${spring.rabbitmq.password}")
    public String RabbitmqPassword;

    @Value("${spring.rabbitmq.virtual-host}")
    private String virtualHost;

    @Bean
    public Queue queue() {
        return new Queue(QUEUE_NAME, true,false,false);
    }

    @Bean
    public DirectExchange exchange() {
        return new DirectExchange(EXCHANGE_NAME,true,false);// 持久化+不自动删除
    }

    @Bean
    public Binding binding(Queue queue, DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }


    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }




}

